package B3;

import java.util.ArrayList;
import java.util.List;

public class StringExtractor {

    // 提取字符串的函数
    public static List<String> extractStrings(String sourceCode) {
        List<String> strings = new ArrayList<>();
        StringBuilder currentString = new StringBuilder();
        boolean inString = false;  // 标记是否处于字符串内
        boolean inEscape = false;  // 标记是否在转义字符状态

        for (int i = 0; i < sourceCode.length(); i++) {
            char currentChar = sourceCode.charAt(i);

            if (inString) {
                if (inEscape) {
                    // 处理转义字符
                    currentString.append(currentChar);
                    inEscape = false;  // 处理完转义字符后，返回到正常读取状态
                } else {
                    if (currentChar == '\\') {
                        // 遇到反斜杠，进入转义字符状态
                        inEscape = true;
                    } else if (currentChar == '"') {
                        // 遇到双引号，结束当前字符串
                        strings.add(currentString.toString());
                        currentString.setLength(0);  // 清空当前字符串
                        inString = false;  // 退出字符串状态
                    } else {
                        // 普通字符，加入当前字符串
                        currentString.append(currentChar);
                    }
                }
            } else {
                // 如果不在字符串内，遇到双引号则进入字符串状态
                if (currentChar == '"') {
                    inString = true;
                }
            }
        }

        return strings;
    }
}
